package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.dto.UserDTO;
import com.neusoft.elmboot.dto.UserPrivacyDTO;
import com.neusoft.elmboot.service.ex.*;
import com.neusoft.elmboot.util.JsonResult;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/users")
public class UserController {
	public static final int OK = 200;
	public static final int DATA_NOT_FOUND = 204;
	public static final int ERROR = 500;
	public static final int DATA_EXIST = 205;
	
	@Autowired
	private UserService userService;

	//用户注册
	@PostMapping
	public JsonResult<User> userRegister(@RequestBody User user) throws Exception{
		JsonResult<User> result = new JsonResult<User>();
		try {
			User data = userService.userRegister(user);
			result = new JsonResult<User>(OK, data);
			result.setMessage("注册成功!");
			return result;
		}catch (UserDuplicationException e){
			result.setState(DATA_EXIST);
			result.setMessage(e.getMessage());
			return result;
		}catch (InsertException e){
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PostMapping("/login")
	public JsonResult<UserDTO> userLogin(@RequestBody User user) throws Exception {
		JsonResult<UserDTO> result = new JsonResult<UserDTO>();
		try {
			User data = userService.userLogin(user.getUserId(), user.getPassword());
			UserDTO userDTO = new UserDTO();
			userDTO.setUserId(data.getUserId());
			userDTO.setUserName(data.getUserName());
			result = new JsonResult<UserDTO>(OK, userDTO);
			result.setMessage("登录成功!");
			return result;
		} catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		} catch (PasswordNotMatch e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}


	@GetMapping("/{userId}")
	public JsonResult<UserDTO> getUserById(@PathVariable String userId) throws Exception {
		JsonResult<UserDTO> result = new JsonResult<UserDTO>();
		try {
			User user = userService.getUserById(userId);
			UserDTO userDTO = new UserDTO();
			userDTO.setUserId(user.getUserId());
			userDTO.setUserName(user.getUserName());
			result = new JsonResult<UserDTO>(OK, userDTO);
			result.setMessage("获取用户信息成功");
			return result;
		} catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PutMapping("/userInfo")
	public JsonResult<UserDTO> changeUserInfo(@RequestBody UserDTO user) throws Exception {
		JsonResult<UserDTO> result = new JsonResult<UserDTO>();
		try {
			UserDTO data = userService.changeUserInfo(user);
			result = new JsonResult<UserDTO>(OK, data);
			result.setMessage("修改用户信息成功!");
			return result;
		} catch (UpdateException | DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}


	@PutMapping("/avatar")
	public JsonResult<Integer> changeAvatar(@RequestParam("userId") String userId, @RequestParam("userImg") MultipartFile file) {
		JsonResult<Integer> result = new JsonResult<Integer>();
		try {
			int data = userService.changeAvatar(userId, file);
			result = new JsonResult<Integer>(OK, data);
			result.setMessage("修改用户头像成功!");
			return result;
		} catch (FileException | UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		} catch (IOException e) {
			result.setState(ERROR);
			result.setMessage("文件上传失败!");
			return result;
		}
    }
	@PutMapping("/passwordModification")
	public JsonResult<UserDTO> changePassword(@RequestBody UserPrivacyDTO user) throws Exception {
		JsonResult<UserDTO> result = new JsonResult<UserDTO>();
		try {
			int data = userService.changePassword(user);
			User user1 = userService.getUserById(user.getUserId());
			UserDTO userDTO = new UserDTO();
			userDTO.setUserId(user1.getUserId());
			userDTO.setUserName(user1.getUserName());
			userDTO.setUserSex(user1.getUserSex());
			result = new JsonResult<UserDTO>(OK, userDTO);
			result.setMessage("修改密码成功!");
			return result;
		} catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		} catch (PasswordNotMatch | UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
    }




}



